/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package folder.helper;

import java.awt.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFileChooser;

/**
 *
 * @author ST
 */
public class ImagePreviewAccessory extends JComponent implements PropertyChangeListener {

    /**
     *
     */
    private static final long serialVersionUID = 2626268910546018039L;
    private int originalPicWidth = 0;
    private int originalPicHeight = 0;
    private int newPicWidth = 0;
    private int newPicHeight = 0;
    private int previewSquareSize = 200;
    private Image image;

    public ImagePreviewAccessory(JFileChooser chooser) {
        // Listen for changes to the selected file
        chooser.addPropertyChangeListener(this);

        // Set a preferred size
        setPreferredSize(new Dimension(previewSquareSize, previewSquareSize));
        this.setBounds(0, 10, 0, 0);
        this.setBorder(BorderFactory.createEtchedBorder());
    }

    // This listener listens for changes to the selected file
    public void propertyChange(PropertyChangeEvent evt) {
        if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(evt.getPropertyName())) {
            // Get the new selected file
            File newFile = (File) evt.getNewValue();

            // Prepare the preview data based on the new selected file
            try {
                image = Toolkit.getDefaultToolkit().getImage(newFile.toString());
                MediaTracker mediaTracker = new MediaTracker(this);
                mediaTracker.addImage(image, 0);
                mediaTracker.waitForID(0);
            } catch (Exception ie) {
            }
            ;
            // Repaint this component
            repaint();
        }
    }

    public void paint(Graphics g) {
        if (image != null) {
            getNewImageSize(image);
            try {
                g.drawImage(image, 10, 0, newPicWidth, newPicHeight, this.getBackground(), null);
            } catch (NullPointerException np) {
            }
        }
    }

    private void getNewImageSize(Image original) {

        float scaleFactor = 0;
        originalPicWidth = original.getWidth(this);
        originalPicHeight = original.getHeight(this);
        if (originalPicWidth >= originalPicHeight) {
            scaleFactor = (float)originalPicWidth / (float)(previewSquareSize-10);
            newPicWidth = previewSquareSize-10;
            newPicHeight =(int) ((float)originalPicHeight / scaleFactor);
        } else if (originalPicHeight > originalPicWidth) {
            scaleFactor = (float)originalPicHeight / (float)previewSquareSize;
            newPicHeight = previewSquareSize;
            newPicWidth = (int)((float)originalPicWidth / scaleFactor);
        }
    }
}